package org.jsola.hr.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhr
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DepAndInfoQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = "部门名称")
    private String depName;

    @ApiParam(value = "员工名称或手机号")
    private String empNameOrPhone;

    @ApiParam(value = "员工名称")
    private String empName;

    @ApiParam(value = "公司id")
    private String companyId;

    @ApiParam(value = "是否只展示当前部门的员工,默认 否")
    private Boolean onlyCurrentDep = false;

    @ApiParam(value = "是否只是用户的员工,默认 否")
    private Boolean onlyUser;


    @ApiParam(value = "当前部门id")
    private String currentDepId;

    @ApiParam(value = "员工状态,true:在职员工,false:已离职员工,默认查询在职员工")
    private Boolean isOnlineOrLeave = true;

    @ApiParam(value = "排序字段", allowMultiple = true, type = "Order", example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;


    /**
     * 添加排序
     *
     * @param propertyName 属性名
     * @param desc         是否降序
     * @return this
     */
    public DepAndInfoQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName, desc));
        return this;
    }

    public String getEmpNameOrPhone() {
        if (!StringUtils.isEmpty(empNameOrPhone)) {
            return empNameOrPhone;
        } else if (!StringUtils.isEmpty(empName)) {
            return empName;
        }
        return empNameOrPhone;
    }
}

